package com.romadan.gui.controller.impl;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;

import org.apache.commons.lang.StringUtils;

import com.romadan.general.settings.PConstants;
import com.romadan.gui.controller.AbstractController;

public class MainWindowController extends AbstractController implements
		Initializable {

	public void initialize(URL arg0, ResourceBundle arg1) {
		try {
			console = this.consoleOutput;
			info("Start program...");
			Tab prepareLinAnalysis = new Tab(PConstants.NAME_LINEAR_ANALYSIS_TAB);
			AnchorPane plaPane = loadAnchorPane(PConstants.PATH_TO_LINEAR_ANALYSIS_TAB, PrepareLinearAnalysisTabController.class);
			prepareLinAnalysis.setContent(plaPane);
			
			Tab resultLinearAnalysis = new Tab(PConstants.NAME_RES_LINEAR_ANALYSIS_TAB);
			AnchorPane rlaPane = loadAnchorPane(PConstants.PATH_TO_RES_LINEAR_ANALYSIS_TAB, ResultLinearAnalysisTabController.class);
			resultLinearAnalysis.setContent(rlaPane);
			resultLinAnalysisTab = resultLinearAnalysis;
			resultLinAnalysisTab.setDisable(true);
			
			Tab nonlinAnalysis = new Tab(PConstants.NAME_NONLINEAR_ANALYSIS_TAB);
			AnchorPane naPane = loadAnchorPane(PConstants.PATH_TO_NONLINEAR_ANALYSIS_TAB, NonlinearAnalysisTabController.class);
			nonlinAnalysis.setContent(naPane);
			nonlinAnalysisTab = nonlinAnalysis;
	
			tabPane.getTabs().addAll(prepareLinAnalysis, resultLinearAnalysis, nonlinAnalysis);
			
			consolePane.expandedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                	if (oldValue && !newValue) {
                		AbstractController.stage.setHeight(PConstants.HEIGHT_WINDOW_EXPANDED);               		
                	} else {
                		AbstractController.stage.setHeight(PConstants.HEIGHT_WINDOW_NOT_EXPANDED);
                	}
                }
            });
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private TabPane tabPane;
	@FXML
	private TextArea consoleOutput;
	@FXML
	private TitledPane consolePane;
	@FXML
	private AnchorPane mainWindow;

	private AnchorPane loadAnchorPane(String fxmlPath, Class<?> controller) throws Exception {
		if (StringUtils.isEmpty(fxmlPath))
			throw new IllegalArgumentException(
					"Path to fxml file cannot be null");

		File f = new File(fxmlPath);
		if (!f.exists())
			throw new IOException("Cannot load file or file is not exist: "
					+ fxmlPath);

		URL fxmlURL = f.toURL();
		FXMLLoader fxmlLoader = new FXMLLoader(fxmlURL);
		fxmlLoader.setController(controller.newInstance());
		fxmlLoader.load();

		AnchorPane pane = fxmlLoader.getRoot();

		return pane;
	}
}
